/*
 * @Author: LAPTOP-STS5K12N\icey 2237665219@qq.com
 * @Date: 2025-01-30 15:19:15
 * @LastEditors: LAPTOP-STS5K12N\icey 2237665219@qq.com
 * @LastEditTime: 2025-01-30 20:05:29
 * @FilePath: \Buzzer\Bsp\src\bsp.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/********************************************************************************
 * @file    bsp.c
 * @author  your name
 * @brief
 *******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "sht10.h"
#include "main.h"
#include "i2c.h"
/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/


#define AHT10_I2C_ADDRESS 0x38

// AHT10 命令
#define AHT10_INIT_CMD 0xE1
#define AHT10_TRIGGER_MEASUREMENT_CMD 0xAC
#define AHT10_READ_STATUS_CMD 0x71
/* Private macro -------------------------------------------------------------*/

/* Private function prototypes ----------------------------------------------*/

/* Private functions --------------------------------------------------------*/

/* Private variables --------------------------------------------------------*/

/* Public variables ---------------------------------------------------------*/
static AHT10_t g_AHT10_vars;

/**
 * @brief AHT10 初始化
 * 
 * @return uint8_t
 * 
 * @author XX
 * @date 2025-01-30
 * @copyright Copyright (c) 2025 
 * @version
 *   V1.0  2025-01-30  XX    First release
 */
uint8_t AHT10_Init(void)
{
    uint8_t cmd[3] = {AHT10_INIT_CMD, 0x08, 0x00}; // 初始化命令
    if (HAL_I2C_Master_Transmit(&hi2c2, AHT10_I2C_ADDRESS << 1, cmd, 3, 100) != HAL_OK)
    {
        return 0; // 初始化失败
    }
    HAL_Delay(10); // 等待初始化完成
    return 1;      // 初始化成功
}

/**
 * @brief 触发 AHT10 测量
 *
 * @return uint8_t
 *
 * @author XX
 * @date 2025-01-30
 * @copyright Copyright (c) 2025
 * @version
 *   V1.0  2025-01-30  XX    First release
 */
uint8_t AHT10_TriggerMeasurement(void)
{
    uint8_t cmd[3] = {AHT10_TRIGGER_MEASUREMENT_CMD, 0x33, 0x00}; // 触发测量命令
    if (HAL_I2C_Master_Transmit(&hi2c2, AHT10_I2C_ADDRESS << 1, cmd, 3, 100) != HAL_OK)
    {
        return 0; // 触发测量失败
    }
    return 1; // 触发测量成功
}

/**
 * @brief 读取 AHT10 数据
 *
 * @param temperature
 * @param humidity
 * @return uint8_t
 *
 * @author XX
 * @date 2025-01-30
 * @copyright Copyright (c) 2025
 * @version
 *   V1.0  2025-01-30  XX    First release
 */
uint8_t AHT10_ReadData(float *temperature, float *humidity)
{
    uint8_t data[6] = {0};
    uint32_t raw_humidity = 0, raw_temperature = 0;

    // 读取 6 字节数据
    if (HAL_I2C_Master_Receive(&hi2c2, AHT10_I2C_ADDRESS << 1, data, 6, 100) != HAL_OK)
    {
        return 0; // 读取失败
    }

    // 检查状态位
    if ((data[0] & 0x80) == 0x80)
    {
        return 0; // 数据未准备好
    }

    // 解析湿度数据
    raw_humidity = ((uint32_t)data[1] << 12) | ((uint32_t)data[2] << 4) | ((uint32_t)data[3] >> 4);
    *humidity = (float)raw_humidity * 100 / 0x100000; // 转换为百分比

    // 解析温度数据
    raw_temperature = (((uint32_t)data[3] & 0x0F) << 16) | ((uint32_t)data[4] << 8) | (uint32_t)data[5];
    *temperature = (float)raw_temperature * 200 / 0x100000 - 50; // 转换为摄氏度

    return 1; // 读取成功
}

/**
 * @brief 获取 AHT10 结构体
 * 
 * @return AHT10_t*
 * 
 * @author XX
 * @date 2025-01-30
 * @copyright Copyright (c) 2025 
 * @version
 *   V1.0  2025-01-30  XX    First release
 */
AHT10_t *Get_AHT10_Vars(void)
{
    return &g_AHT10_vars;
}
/************************ (C) COPYRIGHT Your Company *****END OF FILE****/

